﻿//#include <QtGui/QApplication>
#include <QApplication>
#include "tmainwindow.h"
#include "JistApplication.h"
#include "commandlineparser.h"
#include "JistAniTool.h"

class CommandLineHandler : public CommandLineParser
{
public:
    CommandLineHandler();

    bool quit;
    bool showedVersion;    bool disableOpenGL;

private:
    void showVersion();
	void justQuit();
    void setDisableOpenGL();
	void binarySave();

    // Convenience wrapper around registerOption
    template <void (CommandLineHandler::*memberFunction)()>
    void option(QChar shortName,
                const QString &longName,
                const QString &help)
    {
        registerOption<CommandLineHandler, memberFunction>(this,
                                                           shortName,
                                                           longName,
                                                           help);
    }
};

//} // anonymous namespace


CommandLineHandler::CommandLineHandler()
    : quit(false)
    , showedVersion(false)
    , disableOpenGL(false)
{
    option<&CommandLineHandler::showVersion>(
                QLatin1Char('v'),
                QLatin1String("--version"),
                QLatin1String("Display the version"));

    option<&CommandLineHandler::justQuit>(
                QChar(),
                QLatin1String("--quit"),
                QLatin1String("Only check validity of arguments, "
                              "don't actually load any files"));

    option<&CommandLineHandler::setDisableOpenGL>(
                QChar(),
                QLatin1String("--disable-opengl"),
                QLatin1String("Disable hardware accelerated rendering"));

	option<&CommandLineHandler::binarySave>(
				QChar(),
				QLatin1String("--bin"),
				QLatin1String("binarySave"));
}

void CommandLineHandler::showVersion()
{
    if (!showedVersion) {
        showedVersion = true;
        qWarning() << "Tiled (Qt) Map Editor"
                   << qPrintable(QApplication::applicationVersion());
        quit = true;
    }
}

void CommandLineHandler::justQuit()
{
    quit = true;
}

void CommandLineHandler::binarySave()
{
	g_isBinSave = true;
	quit = true;
}

void CommandLineHandler::setDisableOpenGL()
{
    disableOpenGL = true;
}

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(test_texture);

    JistApplication app(argc, argv);
    // QSetting 을 사용하기 위해
    app.setOrganizationName("AniTool");
    app.setOrganizationDomain("AniTool.net");
    app.setApplicationName("AniTool-test");

    CommandLineHandler commandLine;

    if (!commandLine.parse(QCoreApplication::arguments())){
        return 0;
    }
    //if (commandLine.disableOpenGL)
    //     Preferences::instance()->setUseOpenGL(false);
    if (!commandLine.filesToOpen().isEmpty()) {
       g_CMDLINEMode = true;
    }
    TMainWindow tw;
    tw.setVisible(false);

    //    QObject::connect(&a, SIGNAL(fileOpenRequest(QString)),
    //                     &tw, SLOT(loadStart(QString)));
    // 더블 클릭 또는 커맨드라인 모드
    if (!commandLine.filesToOpen().isEmpty()) {
        // 커맨드라인 모드
        if (commandLine.quit){
            tw.setWindowOpacity(0);
            tw.setCmdVisible(false);
            tw.setAttribute(Qt::WA_WState_Hidden);
            tw.setWindowState(Qt::WindowMinimized);

        }
        foreach (const QString &fileName, commandLine.filesToOpen()){
            tw.loadStart(fileName);
        }
    }
    //else {
    //    tw.openLastFiles();
    //}
    if (commandLine.quit){
        // 저장하기
        tw.on_action_save_triggered();
        tw.close();
        return 0;
    }
    tw.setCmdVisible(true);
    tw.show();

    return app.exec();
}
